
#include <BFont>
#include <BImage>
#include <BPainter>
#include <BMouseEvent>
#include <BChangeEvent>

#include <BSwitcher>
#include "member_BSwitcher.h"
#include "member_BButton.h"

using namespace BWE;

#define member					(*(member_BSwitcher*)_ptr)
#define member_allocate()		_ptr = new member_BSwitcher(this)
#define member_release()		delete (member_BSwitcher*)_ptr

BSwitcher::BSwitcher(const BString& name) : BObject(name)
{
	member_allocate();
}
BSwitcher::~BSwitcher()
{
	member_release();
}

void BSwitcher::addButton(BButton* button)
{
	if (button)
	{
		button->setCheckable(true);
		SwitchItem item;
		item.button = button;
		member.items.append(item);
		member.connect(button, Signal_Checked, &member_BSwitcher::slotButtonChecked);
	}
}

void BSwitcher::removeButton(BButton* button)
{
	if (!button)
		return;
	for (int i = 0; i < member.items.size(); i++)
	{
		SwitchItem& item = member.items[i];
		if (item.button != button)
			continue;
		member.disconnect(button);
		member.items.remove(i);
		break;
	}
}

BButton* BSwitcher::checkedButton() const
{
	return member.checkedButton;
}

void BSwitcher::uncheck()
{
	if (member.checkedButton)
	{
		member.checkedButton->setChecked(false);
	}
}

int BSwitcher::buttonCount() const
{
	return member.items.size();
}
BButton* BSwitcher::button(int index)
{
	SwitchItem item = member.items(index);
	return item.button.ptr();
}
const BButton* BSwitcher::button(int index) const
{
	SwitchItem item = member.items(index);
	return item.button.ptr();
}

